package com.qf.anli.controller;

import com.qf.anli.domain.R;
import com.qf.anli.domain.User;
import com.qf.anli.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user, HttpServletRequest request){

        User loginUser = userService.login(user);

        if(loginUser == null){
            //失败
            return R.error("用户名或密码错误");
        }

        //将登陆后的用户信息存入session
        request.getSession().setAttribute("userInfo",loginUser);
        return R.success(null,"登录成功");
    }
}
